<?php
class LanguageFactory extends MACFactory{

	private static $_instance;
	private $initialized = false;

	/**
	 * @return LanguageFactory
	 */
	public static function getInstance() {
		if(is_null(self::$_instance)){
			self::$_instance = new LanguageFactory();
		}
		return self::$_instance;
	}
	
	
	public function init($lang) {
		$this->logger->info("LanguageFactory::init > lang = $lang");
		
		$langLocale = new Zend_Locale($lang);
		Zend_Registry::set('lang', $lang);
				
		$this->translate = new Zend_Translate(array('adapter'=>'csv','content'=> APPLICATION_PATH.'../languages/' . $lang . '.csv','local'=>$langLocale));
		$this->translate->setLocale($langLocale);

		$this->initialized = true;
		Zend_Registry::set('Zend_Translate', $this->translate);

		return $this->translate;
	}
	
	public function getTranslation($key)
	{
		
		if($this->initialized){
			$translate = Zend_Registry::get('Zend_Translate');
			$label = $translate->translate($key);
		}
		else
		{
			$label = "_".$key;
		}
		
		if(ConfigFactory::getInstance()->getConfig()->application->debug_lang == "true" ||
		   ConfigFactory::getInstance()->getConfig()->application->debug_lang)
		{
		   	$label = "[".$key."=]".$label;
		}
		return $label;
	}
}
